package com.tiantianyuegou.activity;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.R;
import com.tiantianyuegou.adapter.GuanListAdapter;
import com.tiantianyuegou.adapter.MyAdapterYonghu;
import com.tiantianyuegou.constant.Constant;
import com.tiantianyuegou.constant.PingjiaContent;
import com.tiantianyuegou.http.ApiDefine;
import com.tiantianyuegou.model.GoodsModel;
import com.tiantianyuegou.model.PingjiaModel;
import com.tiantianyuegou.util.Content;
import com.tiantianyuegou.util.LoadingDialog;
import com.tiantianyuegou.util.NetworkUtils;
import com.tiantianyuegou.util.ToastUtils;
import com.tiantianyuegou.view.CircleImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户评价
 */
public class YongHuPingJiaActivity extends BaseActivity implements View.OnClickListener {
    //     http://www.zgttyg.com/tiantianyuegou-api/comment/list?tgId=342&page=1 商品评价(无参数)
//     http://www.zgttyg.com/tiantianyuegou-api/collect/add?tmId=342&tgId=130 商品收藏
//    http://www.zgttyg.com/tiantianyuegou-api/collect/del?tmId=342&tgId=130 取消商品收藏

    ImageView img_title;
    TextView text_title;
    private LoadingDialog loading;
    private String TAG = "YongHuPingJiaActivity";
    private boolean isRefresh = true;
    private boolean isLoadMore = false;
    private PullToRefreshGridView refresh;
    private ListView lv;
    private GoogleApiClient client;
    List<PingjiaModel> data = new ArrayList<>();
    private MyAdapterYonghu adapter;
    private String tgId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yong_hu_ping_jia);
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
        text_title=(TextView)findViewById(R.id.text_title);
        img_title=(ImageView)findViewById(R.id.img_title);
        initViews();
        getData();
        text_title.setText("用户评价");
        img_title.setOnClickListener(this);

    }

    @Override
    public void findViewByIds() {
        loading = new LoadingDialog(this);
        img_title = (ImageView) findViewById(R.id.img_title);
        text_title = (TextView) findViewById(R.id.text_title);
        refresh = (PullToRefreshGridView) findViewById(R.id.refresh);
        lv = (ListView) findViewById(R.id.lv);
        //下拉刷新
        ILoadingLayout pullDownRefresh = refresh.getLoadingLayoutProxy(true, false);
        pullDownRefresh.setPullLabel("下拉刷新");//下拉提示
        pullDownRefresh.setRefreshingLabel("正在刷新...");//刷新提示
        pullDownRefresh.setReleaseLabel("松开刷新");//下拉一定距离显示的提示
        //上拉加载
        ILoadingLayout pullUpRefresh = refresh.getLoadingLayoutProxy(false, true);
        pullUpRefresh.setPullLabel("上拉加载");
        pullUpRefresh.setRefreshingLabel("正在刷新...");
        pullUpRefresh.setReleaseLabel("松开加载更多");


    }

    @Override
    public void initListener() {
        text_title.setText("用户评价");
        img_title.setOnClickListener(this);


    }

    /**
     * 通过意图对象获取传递的数据并进行联网请求数据
     */
    public void getData() {
        Intent intent = getIntent();
      tgId = intent.getStringExtra("tgId");
        Log.e(TAG, "获取到传入的数据是=========" + tgId+"=======");


    }

    @Override
    public void initData() {
//        获取传入的数据
        Intent intent = getIntent();
        String tgId1 = intent.getStringExtra("tgId");

//        根据用户id进行联网请求数据
        if (!NetworkUtils.isNetworkAvailable(this)) {
            showMsg("请检查网络");
        }
       loading.showDialog();
       loading.setMsg("正在加载");

        RequestParams params = new RequestParams();
//        TODO 徐传递真正的参数进行联网请求数据
        params.addBodyParameter("tgId", 358+"");
        params.addBodyParameter("page",1+"");
        Log.e(TAG, "网络请求参数是========" + tgId1 +"===========");
        loadData(HttpRequest.HttpMethod.POST, ApiDefine.COMMENT_LIST, params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> response) {
                loading.dismiss();
//                refresh.onRefreshComplete();
                Log.e(TAG, "onSuccess response.result用户列表" +
                        response.result);
                if (TextUtils.isEmpty(response.result)) {
                    showMsg("获取更多评价列表失败");
                } else {
                    try {
                        JSONObject jsonObject = new JSONObject(response.result);
                        String code = jsonObject.getString("code");
                        if (code.equalsIgnoreCase("100")) {
//                            layout_nogoods.setVisibility(View.GONE);
                            JSONArray list = jsonObject.getJSONArray("list");
                            data.clear();
                            for (int i = 0; i < list.length(); i++) {
                                PingjiaModel constant = new PingjiaModel();
                                JSONObject temp = list.getJSONObject(i);
                                String tpName = temp.getString("tgsd_good_color");
                                String tpSalePrice = temp.getString("tgsd_good_size");
                                String tpSalesCount = temp.getString("tgsd_add_date");
                                String tpPic = temp.getString("tgsd_star");
                                String content = temp.getString("tgsd_evaluate");
                                Log.e(TAG, "请求到的数据是" + tpName + tpSalePrice + tpSalesCount + tpPic + content + "========");
                                constant.setName(tpName);
                                constant.setSize(tpSalePrice);
                                constant.setContent(content);
                                constant.setStar(tpPic);
                                constant.setTime(tpSalesCount);
                                constant.setColor(tpName);
                                data.add(constant);

                            }
                            //        为lv设置适配器    TODO 传递请求到的数据并进行展示，用户评价界面
                            MyAdapter adapters = new MyAdapter();
                            lv .setAdapter(adapters);

                        } else {

                            if (code.equalsIgnoreCase("105")) {
                                if (!isLoadMore) {
                                    // showMsg("暂无商品");
                                }

                            }

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        if (isRefresh) {
                            isRefresh = false;
                        } else if (isLoadMore) {
                            isLoadMore = false;
                        }
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                loading.dismiss();
                refresh.onRefreshComplete();
                showMsg("当前网络不可用，请检查网络");
                if (isRefresh) {
                    isRefresh = false;
                } else if (isLoadMore) {
                    isLoadMore = false;
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_title:
                finish();
                break;
        }
    }
    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder vh=null;
            if (convertView == null) {
                convertView = View.inflate(YongHuPingJiaActivity.this, R.layout.item_listview_yonghupingjia, null);
                vh = new ViewHolder();
                vh.iv_user = (CircleImageView) convertView.findViewById(R.id.iv_user);
                vh.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                vh.tv_yonghupingjia = (TextView) convertView.findViewById(R.id.tv_yonghupingjia);
                vh.tv_color = (TextView) convertView.findViewById(R.id.tv_color);
                vh.rb = (RatingBar) convertView.findViewById(R.id.rb);
//                vh.rb = (ImageView) convertView.findViewById(R.id.rb);
                vh.tv_color_value = (TextView) convertView.findViewById(R.id.tv_color_value);
                vh.tv_size_value = (TextView) convertView.findViewById(R.id.tv_size_value);
                convertView.setTag(vh);
            }else {

                vh = (ViewHolder) convertView.getTag();
            }
//        ImageLoader.getInstance().displayImage(ApiDefine.SERVER_URL+model.user,vh.iv_user);
            PingjiaModel model = data.get(position);
           /* if ("".equals(model.content)){
                vh.tv_yonghupingjia.setText("暂无");
            }else{

                vh.tv_yonghupingjia.setText(model.content);
            }*/
            vh.tv_color_value.setText(model.color);
            vh.tv_size_value.setText(model.size);
            Log.e(TAG, "获取到的数据是====" + model.content + model.color + model.size + "============");
//            vh.rb.setImageResource(model.star);
//            vh.rb.setImageIcon(model.star);
            //TODO 代做数据
       //     vh.rb.setRating(Float.parseFloat(model.star));

            return convertView;
        }

        private class ViewHolder {
            CircleImageView iv_user;
            TextView tv_name, tv_time, tv_yonghupingjia, tv_color, tv_color_value, tv_size_value;
//            ImageView rb;
            RatingBar rb;
        }
    }
}
